package com.nexus.all.controller;

import com.nexus.all.dto.CommentDTO;
import com.nexus.all.dto.CommentQueryDTO;
import com.nexus.all.service.CommentService;
import com.nexus.all.vo.CommentVO;
import com.nexus.bean.annotation.PassLogin;
import com.nexus.bean.annotation.valid.AddGroup;
import com.nexus.bean.annotation.valid.QueryGroup;
import com.nexus.bean.model.UserInfoVO;
import com.nexus.bean.query.QueryResultVO;
import com.nexus.bean.result.Result;
import com.nexus.bean.utils.ip.AddressUtils;
import com.nexus.bean.utils.ip.IpUtil;
import com.nexus.web.controller.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@RequestMapping("/comment")
@RestController
@Api(tags = "评论管理")
public class CommentController extends CommonController {

    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }


    @ApiOperation(value = "分页查询评论", notes = "分页查询评论")
    @PostMapping("/queryComment")
    @PassLogin
    public Result<QueryResultVO<CommentVO>> queryComment(@RequestBody @Validated(QueryGroup.class) CommentQueryDTO dto) {
        UserInfoVO user = getUser();
        if (Objects.nonNull(user)) {
            //登录
            dto.setUserId(user.getId());
        }
        return Result.newSuccess(commentService.queryComment(dto));
    }

    @ApiOperation(value = "添加评论", notes = "添加评论")
    @PostMapping("/addComment")
    public Result addComment(@RequestBody @Validated(AddGroup.class) CommentDTO dto) {
        dto.setUserId(getUser().getId());
        //获取ip所属地址
        String realAddress = AddressUtils.getCity(IpUtil.getIp(getRequest()));
        dto.setCreateAddress(realAddress);
        commentService.addComment(dto);
        return Result.newSuccess();
    }
}
